define(
    [ "PDAppDir/properties", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar",
        "PDUtilDir/inputSelect",
        "PDUtilDir/fileupload/multiFileUpload", "Date", "DateCN",
        "css!DateCss" ],
    function(Properties, Util, Tool, Slidebar, inputSelect, MFU) {
        function uploadSupportInit($scope, $compile) {
            return new uploadSupport($scope, $compile);
        }
        function uploadSupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;
        }
        uploadSupport.fn = uploadSupport.prototype;
        uploadSupport.fn.digest = function() {
            this.$scope.$digest();
        }
        uploadSupport.fn.init = function(groupId, groupName) {
            this.entity = {
                travelGroupId: groupId,
                groupName: groupName,
                adultNum: 0,
                adultTicketPrice: 0,
                childNum: 0,
                childTicketPrice: 0,
                stuNum: 0,
                stuTicketPrice: 0,
                oldNum: 0,
                oldTicketPrice: 0,
                otherNum: 0,
                otherTicketPrice: 0,
                groupSubsidy: 0,
                groupMoney: 0,
                carCost: 0
            };
            this.cache = {}
            this.bind = {
                isOk: undefined,
                images: new Array()
            }
        }

        uploadSupport.fn.run = function(groupId, groupName) {
            var _this = this;
            _this.init(groupId, groupName);
            this.slidebars = Slidebar({
                size : 'lg',
                url : getServer()
                + "/static/app/group/guideGroupInfos/views/upload.html",
                $scope : this.$scope,
                $compile : this.$compile,
                afterLoad : function($Panel) {
                    _this.initialization();
                }
            });
        };
        uploadSupport.fn.initialization = function() {
            var _this = this;
            $.ajax({
                url : getServer() + "/flea/hclv/submit/account/selectLineByTravelGroupId.htmls",
                data : {
                    travelGroupId : _this.entity.travelGroupId
                },
                success : function(data) {
                    // console.info(data);
                    _this.entity.lineId = data.data.id;
                    _this.entity.lineName = data.data.name;
                    _this.digest();
                }
            });

            MFU.init({
                placeAt:"billImage",
                allowPhyDelete:true,
                allowDelete:true,
                allowMC:true,
                onUploadSuccess:function(file){
                    $.each(_this.bind.images, function(index, row){
                        if(row.key == file.name){
                            row = $.extend(row, {
                                resouceId: file.swordFileId,
                                path: file.swordFilePath
                            });
                        }
                    });
                    _this.bind.isOk = true;
                    _this.$scope.billUploadForm.isOk.$setViewValue(true);
                },
                onDeleteSavedFile:function(file){
                    if(file == null)
                        return;
                    var i = 0;
                    $.each(_this.bind.images, function(index, row){
                        if(row.resouceId == file.swordFileId){
                            i = index;
                            return
                        }
                    });
                    _this.bind.images.splice(i, 1);

                    if(_this.bind.images.length == 0){
                        _this.bind.isOk = undefined;
                    }
                    _this.$scope.billUploadForm.isOk.$setViewValue(_this.bind.isOk);
                    _this.digest();
                },
                onFilesQueued: function(files){
                    var webUploader = this.webUploader, queuedFiles = this.queuedFiles;
                    $.each(files, function(index, file) {
                        if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                            $.each(queuedFiles, function(index, item) {
                                if(file.id == item.file.id){
                                    item.remove();
                                }
                            });
                            return true;
                        }
                        webUploader.makeThumb( file, function( error, src ) {
                            if ( error ) {
                                Util.alert('不能预览');
                                return;
                            }
                            _this.bind.images.push({
                                key: file.name,
                                src: src
                            });
                            _this.digest();
                        }, 500, 300);
                    });
                    if(queuedFiles.length == 0)
                        Util.alert(ImageSuffixNotSupport);
                    webUploader.upload();
                }
            });
        };
        uploadSupport.fn.save = function() {
            var _this = this, files = new Array();
            $.each(_this.bind.images, function(index, row){
                files.push({
                    resouceId: row.resouceId,
                    path: row.path
                });
            });
            $.ajax({
                url : getServer() + "/flea/hclv/submit/account/createSubmitAccount.htmls",
                data : {
                    'model' : JSON.stringify($.extend({
                        images : files
                    },_this.entity))
                },
                success : function(data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.$scope.grid.refresh();
                    Util.alert(data.message);
                    _this.slidebars.close();
                }
            });
        };
        return uploadSupportInit;
    });